package calc;

public enum Operator {

    ADD {
        @Override
        float calculate(Container chain) {
            System.out.println("ADD operation handled");

            if (chain.prev == null) {
                return 0f + chain.getValue();
            }

            float result = chain.prev.getOP().calculate(chain.prev);

            return (result + chain.getValue());
        }

        @Override
        public char repr() { return '+'; }
    },
    SUBTRACT {
        @Override
        float calculate(Container chain) {
            System.out.println("SUBTRACT operation handled");

            if (chain.prev == null) {
                return 0f - chain.getValue();
            }

            float result = chain.prev.getOP().calculate(chain.prev);

            return (result - chain.getValue());
        }

        @Override
        public char repr() { return '-'; }
    },
    MULTIPLY {
        @Override
        float calculate(Container chain) {
            System.out.println("MULTIPLY operation handled");

            if (chain.prev == null) {
                return 0f * chain.getValue();
            }

            float result = chain.prev.getOP().calculate(chain.prev);

            return (result * chain.getValue());
        }

        @Override
        public char repr() { return '*'; }
    },
    DIVIDE {
        @Override
        float calculate(Container chain) {
            System.out.println("DIVIDE operation handled");
            if (chain.value == 0f) {
                // DIVISION BY 0 AAAA
                System.out.println("DIVIDING BY 0 AAAA");
            }

            if (chain.prev == null) {
                return 0f / chain.getValue();
            }

            float result = chain.prev.getOP().calculate(chain.prev);

            return (result / chain.getValue());
        }

        @Override
        public char repr() { return '/'; }
    };

    abstract float calculate(Container chain);
    abstract public char repr();
}
